//对外提供的发送的方法

import 'package:changxindianzishangwu/http/core/zd_net_adapter.dart';

import 'base/ec_base_request.dart';
import 'core/dio_adapter.dart';

class ZdNet {
  //单例模式
  ZdNet._();
  //初始化
  static ZdNet _instance;
  static ZdNet getInstance() {
    if (_instance == null) {
      _instance = ZdNet._();
    }
    return _instance;
  }

  //外层调用发送请求数据
  Future send(EcBaseRequest request) async {
    EcNetResponse response;
    //自己构建的response对象
    response = await sendRequest(request);
    //调用数据返回
    var result = response.data;
    print("http:ZdNet:send():response:${response}");
    //查询状态码
    var status = response.statusCode;
    print('登录 查询码：$status');
    switch (status) {
      case 200:
        return result;
      default:
        throw Exception(response);
    }
  }

  //底层发送请求
  Future<dynamic> sendRequest(EcBaseRequest request) {
    print("http:ZdNet:sendRequest():url:${request.url()}");
    print("http:ZdNet:sendRequest():param:${request.params}");
    print("http:ZdNet:sendRequest():header:${request.header}");
    //实例化子类对象  调用dio库方法
    EcNetAdapter adapter = DioAdapter();

    return adapter.send(request);
  }
}
